﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Public
{
	public partial class NewPipeShowForm : System.Windows.Forms.Form
	{
		public NewPipeShowForm(string filePath)
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			this.filePath = filePath;
			//base.Icon = Resource.YArchitechIcon;
		}

		private void NewPipeShowForm_Load(object sender, EventArgs e)
		{
			this.txtColor.Text = "█RGB 128 128 128";
			this.txtColor.ForeColor = Color.FromArgb(128, 128, 128);
		}

		private void btnColor_Click(object sender, EventArgs e)
		{
			ColorDialog colorDialog = new ColorDialog();
			colorDialog.AllowFullOpen = true;
			colorDialog.FullOpen = true;
			Color xmldataColor = PipeShowHelper.getXMLDataColor(this.txtColor.Text.ToString());
			if (!xmldataColor.IsKnownColor)
			{
				int[] array = new int[16];
				array[0] = PipeShowHelper.GetIntAboutColor(xmldataColor);
				colorDialog.CustomColors = array;
			}
			colorDialog.Color = xmldataColor;
			DialogResult dialogResult = colorDialog.ShowDialog();
			if (DialogResult.OK == dialogResult)
			{
				string newRGB = PipeShowHelper.getNewRGB(colorDialog.Color.R);
				string newRGB2 = PipeShowHelper.getNewRGB(colorDialog.Color.G);
				string newRGB3 = PipeShowHelper.getNewRGB(colorDialog.Color.B);
				string text = string.Concat(new string[]
				{
					"█RGB ",
					newRGB.ToString(),
					" ",
					newRGB2.ToString(),
					" ",
					newRGB3.ToString()
				});
				this.txtColor.Text = text;
				this.txtColor.ForeColor = colorDialog.Color;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (!Regex.IsMatch(this.txtStart.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("起点标高必须为有效数字！");
				this.txtStart.Focus();
				return;
			}
			if (!Regex.IsMatch(this.txtEnd.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				YJKMessageBox.Show("终点标高必须为有效数字！");
				this.txtEnd.Focus();
				return;
			}
			if (double.Parse(this.txtStart.Text) >= double.Parse(this.txtEnd.Text))
			{
				YJKMessageBox.Show("起点标高应小于终点标高！");
				this.txtStart.Focus();
				return;
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.filePath);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("elevationSet");
			XmlElement xmlElement = xmlDocument.CreateElement("elevation");
			xmlElement.SetAttribute("startElevation", this.txtStart.Text.Trim());
			xmlElement.SetAttribute("endElevation", this.txtEnd.Text.Trim());
			xmlElement.SetAttribute("color", this.txtColor.Text.Trim());
			if (xmlNode != null)
			{
				xmlNode.AppendChild(xmlElement);
			}
			xmlDocument.Save(this.filePath);
			base.DialogResult = DialogResult.OK;
		}

		private void txtStart_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '.' && e.KeyChar != '-')
			{
				e.Handled = true;
			}
		}

		private void txtEnd_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '.' && e.KeyChar != '-')
			{
				e.Handled = true;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void txtColor_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = true;
		}

		private void txtColor_KeyDown(object sender, KeyEventArgs e)
		{
			e.Handled = true;
		}

		private readonly string filePath = "";
	}
}
